import {createVNode, ref} from "vue";
import EveryPageEnum from "@/enums/everyPageEnum";
import axios from "@/utils/axios";
import {message, Modal} from "ant-design-vue";
import {ExclamationCircleOutlined} from "@ant-design/icons-vue";

class SectionInfo{
    sectionId: number | undefined = undefined
    kind: Array<number> = new Array<number>()
}

class CellQueryForm {
    sections: Array<SectionInfo> = [new SectionInfo()]
    status: number | undefined
    checkStatus: number | undefined
    workToday: number | undefined
    cellName: string | undefined
    startPage: number | undefined = 1
    everyPage: number | undefined = EveryPageEnum.small
}

class CellSetForm {
    cellId: number | undefined = -1
    cellName: string | undefined
    status: number | undefined
    kind: number | undefined
    sectionId: number | undefined
}

export default ()=>{
    const cellListId = ref()
    const cellQueryFormRef = ref()
    const cellQueryFormState = ref(new CellQueryForm())
    const cellQueryFormRule = {}

    const showCellSetFormModal = ref(false)
    const showSetCellNameFormModal = ref(false)

    const cellSetFormRef = ref()
    const cellSetFormState = ref(new CellSetForm())
    const cellSetFormRule = {
        sectionId:[{
            required: true,
            message: "请选择归属标段",
            type: 'any',
            trigger: 'change'
        }],
        status:[{
            required: true,
            message: '请选择单元状态',
            type: 'any',
            trigger: 'change'
        }],
        kind:[{
            required: true,
            message: '请选择工艺类型',
            type: 'any',
            trigger: 'change'
        }],
        cellName:[{
            required: true,
            message: '请输入单元名称',
            type: 'any',
            trigger: 'blur'
        }]
    }

    const cellSetNameFormRef = ref()
    const cellSetNameFormState = ref(new CellSetForm())
    const cellSetNameFormRule = {
        cellName:[{
            required: true,
            message: '请输入单元名称',
            type: 'any',
            trigger: 'blur'
        }]
    }

    const cellBaseInfoList:any = ref({})
    const inCellBaseInfo:any = ref()
    const cellBorder:any = ref({})


    /**
     * 获取单元基本信息列表
     * @param old_value 当前查询的数据,若传递,则为分页跳转
     */
    const getCellBaseInfoList = (pageInfo:any)=>{
        return new Promise((resolve, reject)=>{
            let queryParms:any = {}

            if(pageInfo && !(pageInfo instanceof MouseEvent)){ // 若为分页跳转
                Object.assign(queryParms, cellBaseInfoList.value.queryParms)
                queryParms.startPage = pageInfo.current
            }else{
                Object.assign(queryParms, cellQueryFormState.value)
            }

            if(!queryParms.sections[0].sectionId) {
                message.info("请选择归属标段")
                return
            }

            axios.post("/common/findCellInfo",
                queryParms).then((res:any)=>{
                if(res.success){
                    inCellBaseInfo.value = undefined
                    cellBaseInfoList.value = res.data
                    cellBaseInfoList.value.queryParms = queryParms
                    resolve(res)
                }else{
                    message.info(res.msg)
                    reject(false)
                }
            })
        })
    }

    /**
     * 查询指定仓面基本信息
     * @param cellId
     */
    const getCellBaseInfo = (cell:any)=>{
        return new Promise((resolve,reject)=>{
            axios.post("/common/findCellBasicInfoByCellId",
                {cellId:cell.cellId}).then((res:any)=>{
                if(res.success){
                    Object.assign(res.data,cell)
                    inCellBaseInfo.value = res.data
                    resolve(res)
                }else{
                    message.info(res.msg)
                    reject(false)
                }
            })
        })
    }

    /**
     * 打开单元弹框
     */
    const openSetCellModal = ()=>{
        cellSetFormRef.value.resetFields()
        showCellSetFormModal.value = true
    }

    /**
     * 打开修改单元名称弹框
     */
    const openSetCellNameModal = (cellInfo:any)=>{
        cellListId.value = cellInfo.cellId
        // cellSetNameFormRef.value.resetFields()
        cellSetNameFormState.value.cellName = cellInfo.cellName
        showSetCellNameFormModal.value = true
    }

    /**
     * 设置单元信息
     */
    const setCell = (callback: Function|null)=>{
        cellSetFormRef.value.validate().then(()=>{
            axios.post("/common/updateCellInfoByCellId",
                cellSetFormState.value).then((res:any)=>{
                if(res.success){
                    if(callback) callback()
                }else{
                    message.info(res.msg)
                }
            })
        })
    }

    /**
     * 设置单元名称
     */
    const setCellName = (callback: Function|null)=>{
        cellSetNameFormState.value.cellId =parseInt(cellListId.value)
        cellSetNameFormRef.value.validate().then(()=>{
            axios.post("/common/updateCellName",
                cellSetNameFormState.value).then((res:any)=>{
                getCellBaseInfoList(null)
                if(res.success){
                    if(callback) callback()
                }else{
                    message.info(res.msg)
                }
            })
        })
    }

    /**
     * 设置单元状态
     */
    const setCellStatus = (cellInfo:any)=>{
        let status = cellInfo.cellStatus === 0 ? 1 : 0
        axios.post("/common/saveCellStatus",
            {cellId:cellInfo.cellId,status:status}).then((res:any)=>{
            if(res.success){
                cellBaseInfoList.value.cellInfo.find((item:any)=>{
                    return item.cellId==cellInfo.cellId
                },1).cellStatus = status
                message.info(`修改成功`)
            }else{
                message.info(`修改失败,${res.msg}`)
            }
        })
    }

    /**
     * 删除单元
     */
    const delCell = (cellInfo:any)=>{
        Modal.confirm({
            title: `是否删除名称为${cellInfo.cellName}的施工单元?`,
            icon: createVNode(ExclamationCircleOutlined),
            okText: '确定',
            okType: 'danger',
            cancelText: '取消',
            onOk() {
                axios.post("/common/saveCellStatus",
                    {cellId:cellInfo.cellId,status:2}).then((res:any)=>{
                    if(res.success){
                        cellBaseInfoList.value.cellInfo.splice(
                            cellBaseInfoList.value.cellInfo.findIndex((item:any)=>{
                                return item.cellId==cellInfo.cellId
                            },1)
                        )
                        message.info(`删除成功`)
                    }else{
                        message.info(`删除失败,${res.msg}`)
                    }
                })
            }
        });
    }

    /**
     * 获取单元边界
     */
    const getCellBorder = ()=>{

    }

    return{
        cellQueryFormRef,
        cellQueryFormState,
        cellQueryFormRule,
        showCellSetFormModal,
        cellSetFormRef,
        cellSetFormState,
        cellSetFormRule,

        showSetCellNameFormModal,
        cellSetNameFormRef,
        cellSetNameFormState,
        cellSetNameFormRule,

        cellBaseInfoList,
        inCellBaseInfo,
        cellBorder,
        getCellBaseInfoList,
        openSetCellModal,
        openSetCellNameModal,
        setCell,
        setCellName,
        delCell,
        setCellStatus,
        getCellBorder,
        getCellBaseInfo
    }
}